package com.doucat.support.web;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import com.doucat.support.component.fragment.base.ToolbarFragment;
import com.doucat.support.constant.PublicBundleConstant;
import com.tencent.smtt.sdk.WebSettings;
import com.tonly.doucatlibrary.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class WebActivity extends ToolbarFragment {
    private X5WebView mWebView;

    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onRootViewCreate(inflater,container,savedInstanceState);
        setTitleFrameLayout(R.layout.activity_web);
        mWebView=findViewById(R.id.webView);
        WebSettings webSetting = mWebView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(getContext().getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(getContext().getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(getContext().getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);

        Bundle bundle=getArguments();
        if(bundle!=null){
            String url=bundle.getString(PublicBundleConstant.WEB_URL);
            String title=bundle.getString(PublicBundleConstant.URL_TITLE);
//        setTitle(title);
            try {
                if (Build.VERSION.SDK_INT >= 16) {
                    Class<?> clazz = mWebView.getSettings().getClass();
                    Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
                    if (method != null) {
                        method.invoke(mWebView.getSettings(), true);
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //给webview添加Js调用接口，第一个参数为类对象，第二个参数为自定义别名，J
            //s通过这个别名来调用Java的方法，我这里自定义为android。
            mWebView.addJavascriptInterface(WebActivity.this,"android");
            mWebView.loadUrl(url);
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (mWebView != null && mWebView.canGoBack()) {
//                mWebView.goBack();
////                if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 16)
////                    changGoForwardButton(mWebView);
//                return true;
//            } else
//                return super.onKeyDown(keyCode, event);
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    @JavascriptInterface
    public String getUserId(){
//        return getUser().userId;
        return "";
    }
}